package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.exception.BaseException;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.GroupItemMapper;
import com.itheima.mapper.GroupSetmealMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItemVO;
import com.itheima.pojo.CheckPageQueryDTO;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Autowired
    private GroupItemMapper groupItemMapper;
    @Autowired
    private GroupSetmealMapper groupSetmealMapper;

    /**
     * 新增检查组
     * @param checkGroup
     */
    @Transactional
    public void add(CheckGroup checkGroup, List<Integer> checkitemIds) {
        checkGroupMapper.insert(checkGroup);
        Integer id = checkGroup.getId();
        for (Integer itemId : checkitemIds) {
            groupItemMapper.insert(id,itemId);
        }
    }

    /**
     * 检查组批量删除
     * @param ids
     */
    @Transactional
    public void deleteBatch(List<Long> ids) {
        for (Long id : ids) {
            List<Integer> setmealIds = groupSetmealMapper.GetByGroupId(id);
            if (setmealIds.size() != 0) {
                throw new BaseException(MessageConstant.DELETE_CHECKGROUP_FAIL);
            }
        }
        checkGroupMapper.delete(ids);
        groupItemMapper.deleteByGroup(ids);
    }

    /**
     * 根据套餐来查询检查组
     * @param setmealId
     * @return
     */
    public List<Integer> setmealByGroupId(Integer setmealId) {
        List<Integer> setmealIds = groupSetmealMapper.GetByGroupId(Long.valueOf(setmealId));
        return setmealIds;
    }

    /**
     * 检查组分页查询
     * @param queryPageBean
     * @return
     */
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckGroup> page = checkGroupMapper.pageQuery(queryPageBean);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 修改检查组
     * @param checkGroup
     */
    @Transactional
    public void updateById(CheckGroup checkGroup,List<Integer> checkitemIds) {
        checkGroupMapper.update(checkGroup);
        Integer groupId = checkGroup.getId();
        groupItemMapper.deleteByGroupId(groupId);
        for (Integer checkitemId : checkitemIds) {
            groupItemMapper.insert(groupId,checkitemId);
        }
    }

    /**
     * 全部检查组查询
     * @return
     */
    public List<CheckGroup> findAll() {
        List<CheckGroup> groups = checkGroupMapper.findAll();
        return groups;
    }

    /**
     * 根据主键ID进行检查组查询
     * @param id
     * @return
     */
    public CheckGroup geyById(Long id) {
        CheckGroup checkGroup = checkGroupMapper.getById(id);
        return checkGroup;
    }


}
